package com.example.sys.service.impl;

import com.alibaba.fastjson2.JSON;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.utils.JwtUtil;
import com.example.sys.entity.Menu;
import com.example.sys.entity.Role;
import com.example.sys.entity.User;
import com.example.sys.entity.UserRole;
import com.example.sys.mapper.UserMapper;
import com.example.sys.mapper.UserRoleMapper;
import com.example.sys.service.IMenuService;
import com.example.sys.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuage
 * @since 2023-12-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtUtil jwtUtil;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Autowired
    private IMenuService menuService;

    @Override
    public Map<String, Object> login(User user) {
//       根据用户名查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        User loginUser = this.baseMapper.selectOne(wrapper);
//        结果不为空，并将密码和传入密码匹配,则生成token，并将用户信息存入redis
        if (loginUser != null && passwordEncoder.matches(user.getPassword(), loginUser.getPassword())){
//            String key = "user:" + UUID.randomUUID();

//            存入redis
            loginUser.setPassword(null);
//            redisTemplate.opsForValue().s et(key,loginUser,2, TimeUnit.MINUTES);

            //            创建jwt
            String token = jwtUtil.createToken(loginUser);

//            返回数据
            Map<String,Object> data = new HashMap<>();
            data.put("token",token);
            return data;
        }
        return null;
    }

//
//    @Override
//    public Map<String, Object> login(User user) {
////       根据用户名和密码查询
//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.eq("username",user.getUsername());
//        wrapper.eq("password",user.getPassword());
//        User loginUser = this.baseMapper.selectOne(wrapper);
////        结果不为空，则生成token，并将用户信息存入redis
//        if (loginUser != null){
//            String key = "user:" + UUID.randomUUID();
//
////            存入redis
//            loginUser.setPassword(null);
//            redisTemplate.opsForValue().set(key,loginUser,2, TimeUnit.MINUTES);
////            返回数据
//            Map<String,Object> data = new HashMap<>();
//            data.put("token",key);
//            return data;
//        }
//        return null;
//    }

    @Override
    public Map<String, Object> getUserInfo(String token) {
//        根据token获取用户信息
//        Object obj = redisTemplate.opsForValue().get(token);
        User loginUser = null;
        try {
            loginUser = jwtUtil.parseToken(token, User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (loginUser != null){
            Map<String, Object> data = new HashMap<>();
            data.put("name",loginUser.getUsername());
            data.put("avatar",loginUser.getAvatar());
            data.put("userid",loginUser.getId());


            //            角色
            List<String> roleList = this.baseMapper.getRoleNameByUserId(loginUser.getId());
            data.put("roles",roleList);

            //            权限列表
            List<Menu> menuList = menuService.getMenuListByUerId(loginUser.getId());
            data.put("menuList",menuList);

            return data;
        }
        return null;
    }

    @Override
    public void logout(String token) {
    }

    @Override
    @Transactional
    public void addUser(User user) {
//        写入用户表
        this.baseMapper.insert(user);
//        写入用户角色表
        List<Integer> roleIdList = user.getRoleIdList();
        if (roleIdList != null){
            for (Integer roldId :roleIdList){
                userRoleMapper.insert(new UserRole(null,user.getId(),roldId));
            }
        }
    }

    @Override
    public User getUserByID(Integer id) {
        User user = this.baseMapper.selectById(id);

        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<UserRole> userRoleList = userRoleMapper.selectList(wrapper);

        List<Integer> roleIdList = userRoleList.stream()
                .map(userRole -> {return userRole.getRoleId();})
                .collect(Collectors.toList());
        user.setRoleIdList(roleIdList);

        return user;
    }

    @Override
    @Transactional
    public void updateUser(User user) {
//      更新用户表
        this.baseMapper.updateById(user);
//        清除原有角色
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getId());
        userRoleMapper.delete(wrapper);
//        设置新的角色
        List<Integer> roleIdList = user.getRoleIdList();
        if (roleIdList != null){
            for (Integer roldId :roleIdList){
                userRoleMapper.insert(new UserRole(null,user.getId(),roldId));
            }
        }
    }

    @Override
    public void deletUserByID(Integer id) {
        this.baseMapper.deleteById(id);
//        清除原有角色
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        userRoleMapper.delete(wrapper);
    }


}
